package com.cai310.lottery.ticket.protocol.local.disassemble;
import java.lang.reflect.InvocationTargetException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cai310.lottery.Constant;
import com.cai310.lottery.common.Lottery;
import com.cai310.lottery.common.SalesMode;
import com.cai310.lottery.support.dlt.DltCompoundContent;
import com.cai310.lottery.support.dlt.DltPlayType;
import com.cai310.lottery.ticket.dto.PrintInterfaceDTO;
import com.cai310.lottery.ticket.dto.TicketDTO;
import com.cai310.utils.JsonUtil;

public class DltDisassemble extends AbstractDisassemble {
	private static final NumberFormat TWO_NF = new DecimalFormat("00");
	protected Logger logger = LoggerFactory.getLogger(getClass());
	private static final String NUMCODE = ",";
	private static final String CONTENTCONNECTCODE = "-";
	private static final String DANSPLITCODE = ":";
	private static final String SINGSPILTCODE = "+";
	@Override
	protected int getMaxMultiple() {
		return 99;
	}

	@Override
	protected int getMaxUnitsKeno() {
		return 5;
	}

	@Override
	protected int getMaxUnitsCommon() {
		return 5;
	}

	@Override
	protected List<TicketDTO> doCompound(PrintInterfaceDTO printData, Date createTime)
			throws Exception {
		
		List<TicketDTO> resultList = new ArrayList<TicketDTO>();
		
		if(printData.getMode()==SalesMode.COMPOUND){
			String[] contents = JsonUtil.getStringArray4Json(printData.getContent());
			for(String content:contents){
				Byte BetType=null;
				Integer PlayType=null;
				Integer ISAPPEND = 0;		
				StringBuffer BetContent = new StringBuffer();
				DltCompoundContent dltCompoundContent = JsonUtil.getObject4JsonString(content, DltCompoundContent.class);
				if(printData.getBetType()==DltPlayType.Select12to2.ordinal()){//12选2
					    BetType=1;
					    if(dltCompoundContent.getUnits()==1){
					    	PlayType=1;;//胆码类型
					    	BetContent.append(StringUtils.join(formatBetNum(dltCompoundContent.getBlueDanList(),TWO_NF),  NUMCODE));
					    	BetContent.append(StringUtils.join(formatBetNum(dltCompoundContent.getBlueList(),TWO_NF),  NUMCODE));
					    }else{
					    	PlayType=2;;//胆码类型
					    	BetContent.append(StringUtils.join(formatBetNum(dltCompoundContent.getBlueDanList(),TWO_NF),  NUMCODE)).append(DANSPLITCODE);
							BetContent.append(StringUtils.join(formatBetNum(dltCompoundContent.getBlueList(),TWO_NF),  NUMCODE));
					    }			
				}else{
					   if(printData.getBetType()==DltPlayType.GeneralAdditional.ordinal()){//追加方案
								ISAPPEND=1;
					   }
					   BetType=0;
					   if(dltCompoundContent.getUnits()==1){
						    PlayType=1;;//
						    BetContent.append(StringUtils.join(formatBetNum(dltCompoundContent.getRedDanList(),TWO_NF),  NUMCODE));
							BetContent.append(StringUtils.join(formatBetNum(dltCompoundContent.getRedList(),TWO_NF),  NUMCODE)).append(CONTENTCONNECTCODE);
							BetContent.append(StringUtils.join(formatBetNum(dltCompoundContent.getBlueDanList(),TWO_NF),  NUMCODE));
							BetContent.append(StringUtils.join(formatBetNum(dltCompoundContent.getBlueList(),TWO_NF),  NUMCODE));
					   }else{
					    	PlayType=2;;//胆码类型
					    	BetContent.append(StringUtils.join(formatBetNum(dltCompoundContent.getRedDanList(),TWO_NF),  NUMCODE)).append(DANSPLITCODE);
							BetContent.append(StringUtils.join(formatBetNum(dltCompoundContent.getRedList(),TWO_NF),  NUMCODE)).append(DANSPLITCODE);
							BetContent.append(StringUtils.join(formatBetNum(dltCompoundContent.getBlueDanList(),TWO_NF),  NUMCODE)).append(DANSPLITCODE);
							BetContent.append(StringUtils.join(formatBetNum(dltCompoundContent.getBlueList(),TWO_NF),  NUMCODE));
					    }
						
				}
				this.createTicketDTONew(resultList, printData, BetContent.toString(), dltCompoundContent.getUnits(), BetType, PlayType,ISAPPEND);
			}					
		}
		return resultList;
	}
	/**
	 * 单式内容拆单操作 可由子类覆写做相应的格式化操作
	 */
	@Override
	protected List<TicketDTO> doSingle(PrintInterfaceDTO printData, Date createTime) throws IllegalAccessException,
			InvocationTargetException, NoSuchMethodException {
		List<TicketDTO> resultList = new ArrayList<TicketDTO>();
		TicketDTO ticketNew = null;
		Byte BetType=null;
		Integer PlayType=null;
		Integer ISAPPEND = 0;		
		if(printData.getBetType()==DltPlayType.Select12to2.ordinal()){//12选2
		    BetType=1;
		    PlayType=1;
	    }else{
		    BetType=0;
		    PlayType=1;;//
		    if(printData.getBetType()==DltPlayType.GeneralAdditional.ordinal()){//追加方案
				ISAPPEND=1;
			}
	    }
		
		
		
		int maxUnits = Lottery.isKeno(printData.getLotteryType()) ? this.getMaxUnitsKeno() : this.getMaxUnitsCommon();
		if (printData.getUnits() > maxUnits) {
			String[] arr = printData.getContent().split("(\r\n|\n)+");
			int units = 0;
			List<String> contentList = new ArrayList<String>();
			for (String line : arr) {
				String[] bet = line.split(Constant.SINGLE_SEPARATOR_FOR_NUMBER);
				if(bet.length==7){
					contentList.add(bet[0]+NUMCODE+bet[1]+NUMCODE+bet[2]+NUMCODE+bet[3]+NUMCODE+bet[4]+CONTENTCONNECTCODE+bet[5]+NUMCODE+bet[6]);
				}else{
					contentList.add(StringUtils.join(bet, NUMCODE));
				}
				units++;
				if (units == maxUnits) {
					ticketNew = new TicketDTO();
					PropertyUtils.copyProperties(ticketNew, printData);
					ticketNew.setId(null);
					ticketNew.setPrintinterfaceId(printData.getId());
					ticketNew.setCreateTime(createTime);
					ticketNew.setUnits(units);
					ticketNew.setSchemeCost(units * unitsMoney * ticketNew.getMultiple());
					ticketNew.setContent(StringUtils.join(contentList, SINGSPILTCODE));
					ticketNew.setBetType(BetType);
					ticketNew.setExtension(ISAPPEND+"-"+PlayType);
					resultList.addAll(this.doMultiple(ticketNew));
					units = 0;// 计数复位
					contentList.clear();// 重置
				}
			}
			if (units > 0) {
				ticketNew = new TicketDTO();
				PropertyUtils.copyProperties(ticketNew, printData);
				ticketNew.setId(null);
				ticketNew.setPrintinterfaceId(printData.getId());
				ticketNew.setCreateTime(createTime);
				ticketNew.setUnits(units);
				ticketNew.setBetType(BetType);
				ticketNew.setExtension(ISAPPEND+"-"+PlayType);
				ticketNew.setSchemeCost(units * unitsMoney * ticketNew.getMultiple());
				ticketNew.setContent(StringUtils.join(contentList, SINGSPILTCODE));
				resultList.addAll(this.doMultiple(ticketNew));
			}
		} else {
			ticketNew = new TicketDTO();
			PropertyUtils.copyProperties(ticketNew, printData);
			List<String> contentList = new ArrayList<String>();
			String[] arr = printData.getContent().split("(\r\n|\n)+");
			for (String line : arr) {
				String[] bet = line.split(Constant.SINGLE_SEPARATOR_FOR_NUMBER);
				if(bet.length==7){
					contentList.add(bet[0]+NUMCODE+bet[1]+NUMCODE+bet[2]+NUMCODE+bet[3]+NUMCODE+bet[4]+CONTENTCONNECTCODE+bet[5]+NUMCODE+bet[6]);
				}else{
					contentList.add(StringUtils.join(bet, NUMCODE));
				}
			}
			ticketNew.setContent(StringUtils.join(contentList, SINGSPILTCODE));
			ticketNew.setId(null);
			ticketNew.setBetType(BetType);
			ticketNew.setExtension(ISAPPEND+"-"+PlayType);
			ticketNew.setPrintinterfaceId(printData.getId());
			ticketNew.setCreateTime(createTime);
			resultList.addAll(this.doMultiple(ticketNew));
		}
		return resultList;
	}
	/**
	 * 
	 * @param printData
	 * @param createTime
	 * @param contentList
	 * @param playType
	 * @return
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws NoSuchMethodException
	 */
	private void createTicketDTONew(List<TicketDTO> resultList ,PrintInterfaceDTO printData,String content,Integer units,Byte betType,Integer playType,Integer ISAPPEND) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException{
		TicketDTO ticketNew = new TicketDTO();
		PropertyUtils.copyProperties(ticketNew, printData);
		ticketNew.setId(null);
		ticketNew.setPrintinterfaceId(printData.getId());
		ticketNew.setContent(content);
		ticketNew.setUnits(units);
		ticketNew.setBetType(betType);
		ticketNew.setExtension(ISAPPEND+"-"+playType);
		ticketNew.setSchemeCost(units * unitsMoney * ticketNew.getMultiple());
		ticketNew.setCreateTime(new Date());
		resultList.addAll(super.doMultiple(ticketNew));
	}
	

	
}
